home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / samples / newdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  10.5 KB  |  407 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. /*
  20.  *  newdemo.c    -    A demo program using PDCurses. The program illustrate
  21.  *               the use of colours for text output.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <signal.h>
  26. #include <time.h>
  27. #include <curses.h>
  28.  
  29. #if defined(XCURSES)
  30.     char *XCursesProgramName = "newdemo";
  31. #endif
  32.  
  33. #ifdef PDCDEBUG
  34. char *rcsid_newdemo  = "$Id$";
  35. #endif
  36.  
  37. /*
  38.  *  The Australian map
  39.  */
  40. char    *AusMap[16] =
  41. {
  42.     "           A           A ",
  43.     "    N.T. AAAAA       AAAA ",
  44.     "     AAAAAAAAAAA  AAAAAAAA ",
  45.     "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
  46.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  47.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  48.     " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  49.     "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
  50.     "W.A. AAAAAAAAA      AAAAAA Vic.",
  51.     "       AAA   S.A.     AA",
  52.     "                       A  Tas.",
  53.     ""
  54. };
  55.  
  56. /*
  57.  *  Funny messages
  58.  */
  59. #define NMESSAGES   6
  60.  
  61. char    *messages[] =
  62. {
  63.     "Hello from the Land Down Under",
  64.     "The Land of crocs. and a big Red Rock",
  65.     "Where the sunflower runs along the highways",
  66.     "the dusty red roads lead one to loneliness",
  67.     "Blue sky in the morning and",
  68.     "freezing nights and twinkling stars",
  69.     ""
  70. };
  71.  
  72. /*
  73.  *  Main driver
  74.  */
  75. main()
  76. {
  77. WINDOW  *win;
  78. int     w, x, y, i, j, len;
  79. time_t  t;
  80. char    buffer[80], *message;
  81. int     width, height;
  82. chtype  save[80], ch;
  83. void    trap();
  84.  
  85.     initscr();
  86.     start_color();
  87.     cbreak();
  88.     signal(SIGINT, trap);
  89.     noecho();
  90.  
  91. /*  refresh stdscr so that reading from it will not cause it to overwrite */
  92. /*  the other windows that are being created */
  93.  
  94.     refresh();
  95.  
  96.     width  = 48;
  97.     height = 14;                /* Create a drawing window */
  98.     win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
  99.     if(win == NULL)
  100.     {   endwin();
  101.         return 1;
  102.     }
  103.  
  104.     while(TRUE)
  105.     {   init_pair(1,COLOR_WHITE,COLOR_BLUE);
  106. /*        wattrset(win, COLOR_PAIR(1));*/
  107.         wbkgd(win, COLOR_PAIR(1));
  108.         werase(win);
  109.  
  110.         init_pair(2,COLOR_RED,COLOR_RED);
  111.         wattrset(win, COLOR_PAIR(2));
  112.         ch = ' ';
  113.         box(win, ch, ch);
  114.         wrefresh(win);
  115.         wattrset(win, 0);
  116.                                 /* Do ramdom output of a character */
  117.         ch = 'a';
  118.         nodelay(stdscr,TRUE);
  119.         for(i=0; i < 5000; ++i)
  120.         {   x = rand() % (width-2)  + 1;
  121.             y = rand() % (height-2) + 1;
  122.             mvwaddch(win, y, x, ch);
  123.             wrefresh(win);
  124.             if (getch() != ERR)
  125.                 break;
  126.             if(i == 2000)
  127.             {   ch = 'b';
  128.                 init_pair(3,COLOR_CYAN,COLOR_YELLOW);
  129.                 wattron(win, COLOR_PAIR(3));
  130.             }
  131.         }
  132.         nodelay(stdscr,FALSE);
  133.  
  134.         SubWinTest(win);
  135.                                 /* Erase and draw green window */
  136.         init_pair(4,COLOR_YELLOW,COLOR_GREEN);
  137. /*        wattrset(win, COLOR_PAIR(4) | A_BOLD);*/
  138.         wbkgd(win, COLOR_PAIR(4) );
  139.         wattrset(win, A_BOLD);
  140.         werase(win);
  141.         wrefresh(win);
  142.                                 /* Draw RED bounding box */
  143.         wattrset(win, COLOR_PAIR(2));
  144.         box(win, ' ', ' ');
  145.         wrefresh(win);
  146.                                 /* Display Australia map */
  147.         wattrset(win, A_BOLD);
  148.         i = 0;
  149.         while(*AusMap[i])
  150.     {   mvwaddstr(win, i+1, 8, AusMap[i]);
  151.             wrefresh(win);
  152.             napms(100);
  153.             ++i;
  154.         }
  155.  
  156.         init_pair(5,COLOR_BLUE,COLOR_WHITE);
  157.         wattrset(win, COLOR_PAIR(5) | A_BLINK);
  158.     mvwaddstr(win, height-2, 4, " PDCurses 2.2 for DOS, OS/2 and Xwindows");
  159.     wrefresh(win);
  160.  
  161.                 /* Draw running messages */
  162.     init_pair(6,COLOR_YELLOW,COLOR_WHITE);
  163.     wattrset(win, COLOR_PAIR(6));
  164.     message = messages[0];
  165.     len = strlen(message);
  166.     j = 0;
  167.     i = 2;
  168.     w = width-2;
  169.     nodelay(stdscr,TRUE);
  170.         while(j < NMESSAGES)
  171.         {   strncpy(buffer, message, w - i);
  172.             buffer[w-i] = 0;
  173.         mvwaddstr(win, height/2, i, buffer);
  174.             if(w - i < len)
  175.             {   memset(buffer, ' ', i);
  176.                 strcpy(buffer, message + (w - i));
  177.                 buffer[strlen(buffer)]   = ' ';
  178.                 buffer[i-2] = '\0';
  179.                 mvwaddstr(win, height/2, 2, buffer);
  180.         }
  181.             wrefresh(win);
  182.             if (getch() != ERR)
  183.             {   flushinp();
  184.         break;
  185.             }
  186.             mvwaddch(win, height/2, i, ' ');
  187.             i = ++i % w;
  188.             if(i < 2)
  189.             {   message = messages[++j%NMESSAGES];
  190.                 memset(buffer, ' ', w-2);
  191.         buffer[w-2] = 0;
  192.                 mvwaddstr(win, height/2, 2, buffer);
  193.                 i = 2;
  194.             }
  195.         napms(300);
  196.         }
  197.     nodelay(stdscr,FALSE);
  198.  
  199.         j = 0;
  200.                                 /*  Draw running As across in RED */
  201.         init_pair(7,COLOR_RED,COLOR_GREEN);
  202.         wattron(win, COLOR_PAIR(7));
  203.     for(i=2; i < width - 4; ++i)
  204.         {   ch = mvwinch(win, 4, i);
  205.             save[j++] = ch;
  206.             ch = ch & 0x7f;
  207.         mvwaddch(win, 4, i, ch);
  208.         }
  209.         wrefresh(win);
  210.  
  211.                                 /* Put a message up wait for a key */
  212.         i = height-2;
  213.         wattrset(win, COLOR_PAIR(5));
  214.     mvwaddstr(win, i, 5, " Type a key to continue or ESC to quit ");
  215.         wrefresh(win);
  216.  
  217.         if(WaitForUser() == '\033')
  218.         break;
  219.  
  220.         j = 0;                  /* Restore the old line */
  221.     wattrset(win,0);
  222.         for(i=2; i < width - 4; ++i)
  223.             mvwaddch(win, 4, i, save[j++]);
  224.         wrefresh(win);
  225.  
  226.     BouncingBalls(win);
  227.                                 /* Put a message up wait for a key */
  228.         i = height-2;
  229.         wattrset(win, COLOR_PAIR(5));
  230.     mvwaddstr(win, i, 5, " Type a key to continue or ESC to quit ");
  231.         wrefresh(win);
  232.         if(WaitForUser() == '\033')
  233.             break;
  234.     }
  235. exit:
  236.     endwin();
  237.     return 0;
  238. }
  239.  
  240. /*
  241.  * Test sub windows
  242.  */
  243. SubWinTest(WINDOW *win)
  244. {
  245. int     w, h, sw, sh, bx, by;
  246. WINDOW  *swin1, *swin2, *swin3;
  247.  
  248.     wattrset(win, 0);
  249.     w  = win->_maxx;
  250.     h  = win->_maxy;
  251.     bx = win->_begx;
  252.     by = win->_begy;
  253.     sw = w / 3;
  254.     sh = h / 3;
  255.     if((swin1 = derwin(win, sh, sw, 3, 5)) == NULL)
  256.         return  1;
  257.     if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
  258.         return  1;
  259.     if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
  260.     return  1;
  261.  
  262.     init_pair(8,COLOR_RED,COLOR_BLUE);
  263. /*    wattrset(swin1, COLOR_PAIR(8));*/
  264.     wbkgd(swin1, COLOR_PAIR(8));
  265.     werase(swin1);
  266.     mvwaddstr(swin1, 0, 3, "Sub-window 1");
  267.     wrefresh(swin1);
  268.  
  269.     init_pair(9,COLOR_CYAN,COLOR_MAGENTA);
  270. /*    wattrset(swin2, COLOR_PAIR(9));*/
  271.     wbkgd(swin2, COLOR_PAIR(9));
  272.     werase(swin2);
  273.     mvwaddstr(swin2, 0, 3, "Sub-window 2");
  274.     wrefresh(swin2);
  275.  
  276.     init_pair(10,COLOR_YELLOW,COLOR_GREEN);
  277. /*    wattrset(swin3, COLOR_PAIR(10));*/
  278.     wbkgd(swin3, COLOR_PAIR(10));
  279.     werase(swin3);
  280.     mvwaddstr(swin3, 0, 3, "Sub-window 3");
  281.     wrefresh(swin3);
  282.  
  283.     delwin(swin1);
  284.     delwin(swin2);
  285.     delwin(swin3);
  286.     WaitForUser();
  287.     return  0;
  288. }
  289.  
  290. /*
  291.  *  Bouncing balls
  292.  */
  293. BouncingBalls(WINDOW *win)
  294. {
  295. chtype     c1, c2, c3;
  296. int    w, h;
  297. int     x1, y1, xd1, yd1;
  298. int     x2, y2, xd2, yd2;
  299. int     x3, y3, xd3, yd3;
  300.  
  301.     wbkgd( win, COLOR_PAIR(1) );
  302.     wrefresh(win);
  303.  
  304.     w    = win->_maxx;
  305.     h    = win->_maxy;
  306.     x1   = 2 + rand() % (w - 4);
  307.     y1   = 2 + rand() % (h - 4);
  308.     x2   = 2 + rand() % (w - 4);
  309.     y2   = 2 + rand() % (h - 4);
  310.     x3   = 2 + rand() % (w - 4);
  311.     y3   = 2 + rand() % (h - 4);
  312.     xd1  = 1; yd1 = 1;
  313.     xd2  = 1; yd2 = 0;
  314.     xd3  = 0; yd3 = 1;
  315.     nodelay(stdscr,TRUE);
  316.     while(getch() == ERR)
  317.     {   x1 = xd1 > 0 ? ++x1 : --x1;
  318.         if(x1 <= 1 || x1 >= w - 2)
  319.             xd1 = xd1 ? 0 : 1;
  320.         y1 = yd1 > 0 ? ++y1 : --y1;
  321.         if(y1 <= 1 || y1 >= h - 2)
  322.         yd1 = yd1 ? 0 : 1;
  323.  
  324.         x2 = xd2 > 0 ? ++x2 : --x2;
  325.         if(x2 <= 1 || x2 >= w - 2)
  326.             xd2 = xd2 ? 0 : 1;
  327.         y2 = yd2 > 0 ? ++y2 : --y2;
  328.         if(y2 <= 1 || y2 >= h - 2)
  329.             yd2 = yd2 ? 0 : 1;
  330.  
  331.         x3 = xd3 > 0 ? ++x3 : --x3;
  332.         if(x3 <= 1 || x3 >= w - 2)
  333.         xd3 = xd3 ? 0 : 1;
  334.         y3 = yd3 > 0 ? ++y3 : --y3;
  335.         if(y3 <= 1 || y3 >= h - 2)
  336.             yd3 = yd3 ? 0 : 1;
  337.  
  338.         c1 = mvwinch(win, y1, x1);
  339.         c2 = mvwinch(win, y2, x2);
  340.         c3 = mvwinch(win, y3, x3);
  341.  
  342.         init_pair(8,COLOR_RED,COLOR_GREEN);
  343.         wattrset(win, COLOR_PAIR(8));
  344.     mvwaddch(win, y1, x1, 'O');
  345.         init_pair(9,COLOR_BLUE,COLOR_RED);
  346.         wattrset(win, COLOR_PAIR(9));
  347.         mvwaddch(win, y2, x2, '*');
  348.         init_pair(10,COLOR_YELLOW,COLOR_WHITE);
  349.         wattrset(win, COLOR_PAIR(10));
  350.         mvwaddch(win, y3, x3, '@');
  351.         wmove(win, 0, 0);
  352.         wrefresh(win);
  353.         wattrset(win,0);
  354.     mvwaddch(win, y1, x1, c1);
  355.     mvwaddch(win, y2, x2, c2);
  356.     mvwaddch(win, y3, x3, c3);
  357.     napms(150);
  358.     }
  359.     nodelay(stdscr,FALSE);
  360.     return 0;
  361. }
  362.  
  363. /*
  364.  *  Wait for user
  365.  */
  366. int WaitForUser()
  367. {
  368.  time_t  t;
  369.  chtype ch;
  370.  
  371.     nodelay(stdscr,TRUE);
  372.     t = time((time_t *)0);
  373.     while(1)
  374.       {   
  375.        if ((ch = getch()) != ERR)
  376.          {   
  377.           if (ch == '\033') 
  378.             {
  379.              nodelay(stdscr, FALSE);
  380.              return  ch;
  381.             }
  382.           else 
  383.             {
  384.              nodelay(stdscr, FALSE);
  385.              return  0;
  386.             }
  387.          }
  388.        if (time((time_t *)0) - t > 5) 
  389.          {
  390.           nodelay(stdscr, FALSE);
  391.           return  0;
  392.          }
  393.       }
  394. }
  395.  
  396. /*
  397.  *  Trap interrupt
  398.  */
  399. void trap()
  400. {
  401.     endwin();
  402.     exit(0);
  403. }
  404.  
  405. /*  End of NEWDEMO.C */
  406.